$(function(){
	
	var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
 
        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: true        // clear all form fields after successful submit 

    }; 
 

    $('#form').submit(function() {

        //设置权限
        setGroupRoles();

        $(this).ajaxSubmit(options);

        return false;
    });


    //全选
    var $check_all = $("#check_all");
    var $check_roles = $("input[name='roles']");

    $check_all.click(function(){

        var checked = false;
        if($(this).is(':checked')){
            checked = true;
        }

        $check_roles.each(function(i){
            var $check = $($check_roles[i]);

            if(checked){
                if(!$check.is(':checked')){
                    $check.click();
                }
            }else{
                if($check.is(':checked')){
                    $check.click();
                }
            }
        });

    });

});


function showRequest(formData, jqForm, options) { 
    //var queryString = $.param(formData); 
    return true; 
} 

function showResponse(data, statusText, xhr, $form)  { 
	if(data.success){
		alert(data.message);
		location.href= '/index.php/Admin/AdminGroups/';
	}else{
		alert(data.message);
	}
}

//设置权限
function setGroupRoles(){

    var $check_roles = $("input[name='roles']:checked");

    var roles = new Array();
    $check_roles.each(function(){
        roles.push($(this).val());
    });

    $('#group_roles').val(roles.toString());
}
